using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StarterPackUI : BaseStarterPackUI
{
	public float defaultRewardScale = 1f;

	public float rewardScale_1_2 = 2f;

	public float rewardScale_3 = 1.5f;

	public float defaultCellSize = 140f;

	public float cellSize_1_2 = 277f;

	public float cellSize_3 = 185f;

	public UIGrid doubleGrid_1;

	public UIGrid doubleGrid_2;

	public GameObject blockScreen;

	private Coroutine waitAndShowCoroutine;

	public void Show(StarterPack starterPack)
	{
		if (waitAndShowCoroutine != null)
		{
			CoroutineRunner.Instance.StopCoroutine(waitAndShowCoroutine);
		}
		waitAndShowCoroutine = CoroutineRunner.Instance.StartCoroutine(WaitAndShow(starterPack));
	}

	private IEnumerator WaitAndShow(StarterPack starterPack)
	{
		while ((PanelEndGameRewardUI.instance != null && PanelEndGameRewardUI.instance.IsShow) || (ReputationProductRewardWindow.instance != null && ReputationProductRewardWindow.instance.isActive) || (TemporalProductEndTimeWindow.instance != null && TemporalProductEndTimeWindow.instance.window.activeSelf))
		{
			yield return null;
		}
		if (!starterPack.IsGood())
		{
			yield break;
		}
		blockScreen.SetActive(false);
		base.gameObject.SetActive(true);
		Init(starterPack);
		SetRewards();
		int errorsCount = 0;
		foreach (StarterPackReward starterPackReward in starterPack.rewards)
		{
			if (starterPackReward.rewardGroup == StarterPackRewardGroup.Product && starterPackReward.alternativeReward.rewardGroup == RewardGroups.Non)
			{
				if (errorsCount == 0)
				{
					BCWDebug.LogError(" **************************** No Alternative Reward! starter pack ID: " + starterPack.ID);
				}
				BCWDebug.LogError(string.Concat(" ************** category: ", starterPackReward.category, "  subCategory: ", starterPackReward.subCategory, "  rareType: ", starterPackReward.rareType.ToString()));
				errorsCount++;
			}
		}
	}

	public void Hide()
	{
		starterPack = null;
		blockScreen.SetActive(false);
		base.gameObject.SetActive(false);
		if (waitAndShowCoroutine != null)
		{
			CoroutineRunner.Instance.StopCoroutine(waitAndShowCoroutine);
		}
	}

	public void SetRewards()
	{
		base.CleadRewards();
		CoroutineRunner.Invoke(1f, delegate
		{
			dafaultRewardGrid.repositionNow = true;
			doubleGrid_1.repositionNow = true;
			doubleGrid_2.repositionNow = true;
		});
		float scale = 1f;
		float num = defaultCellSize;
		if (starterPack.rewards.Count < 4)
		{
			if (starterPack.rewards.Count <= 2)
			{
				scale = rewardScale_1_2;
				num = cellSize_1_2;
			}
			else if (starterPack.rewards.Count == 3)
			{
				scale = rewardScale_3;
				num = cellSize_3;
			}
			dafaultRewardGrid.cellHeight = num;
			dafaultRewardGrid.cellWidth = num;
			base.SetRewards(scale, dafaultRewardGrid, starterPack.rewards, false);
		}
		else
		{
			scale = defaultRewardScale;
			num = defaultCellSize;
			doubleGrid_1.cellHeight = num;
			doubleGrid_2.cellWidth = num;
			List<StarterPackReward> list = new List<StarterPackReward>();
			List<StarterPackReward> list2 = new List<StarterPackReward>();
			for (int i = 0; i < starterPack.rewards.Count; i++)
			{
				if ((float)i < Mathf.Ceil((float)starterPack.rewards.Count / 2f))
				{
					list.Add(starterPack.rewards[i]);
				}
				else
				{
					list2.Add(starterPack.rewards[i]);
				}
			}
			base.SetRewards(scale, doubleGrid_1, list, false);
			base.SetRewards(scale, doubleGrid_2, list2, false);
		}
		dafaultRewardGrid.repositionNow = true;
		doubleGrid_1.repositionNow = true;
		doubleGrid_2.repositionNow = true;
	}

	public void BuyStarterPack()
	{
		if (starterPack != null && StarterPacksManager.instance.TryPurchaseStarterPackIAP(starterPack))
		{
			blockScreen.SetActive(true);
			CoroutineRunner.Instance.StartCoroutine(DisableAfterDelay(7f));
		}
	}

	private IEnumerator DisableAfterDelay(float delay)
	{
		yield return new WaitForSeconds(delay);
		blockScreen.SetActive(false);
	}
}
